// pages/me/account/account.js
import http from "../../../utils/http.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabTop: '',
    tabsFixed: null,
    memberAccount: null,
    current: -1,
    pageSize: 10,
    pageNumber: 1,
    allLoaded: false,
    list: [],
    bizList: []
  },

  updateApp: function(e) {
    var index = e.currentTarget.dataset.serial;
    var status = e.currentTarget.dataset.status;
    if (status == 1) {
      wx.navigateTo({
        url: '/pages/trade/apply/apply?applicationId=' + index
      })
    } else {
      wx.navigateTo({
        url: '/pages/finished/detail/detail?applicationId=' + index
      })
    }
  },

  // 跳转详情
  goDetail:function(e){
    console.info(e);
    let remark = e.currentTarget.dataset.remark;
    let operateTime = e.currentTarget.dataset.time;
    let operator = e.currentTarget.dataset.operator;
    let serialNo = e.currentTarget.dataset.serial;
    let operateType = e.currentTarget.dataset.type;
    let amount = e.currentTarget.dataset.amount;
    let url = './accountDetail/accountDetail?remark='+remark + '&operateTime=' + operateTime + '&operator=' + operator +
      '&serialNo=' + serialNo + '&operateType=' + operateType + '&amount=' + amount;
    wx.navigateTo({
      url: url
    })

  },
  handleChange({
    detail
  }) {
    console.log(detail)
    this.setData({
      current: detail.key,
      list: [],
      pageNumber: 1,
      allLoaded:false
    });
    this.loadData();

  },

  //滚动到底部触发事件  
  searchScrollLower: function() {
    // console.log('loading----------')
    // console.log(this.data.allLoaded)
    // console.log(this.data.pageNumber)
    if (!this.data.allLoaded) {
      this.setData({
        pageNumber: this.data.pageNumber + 1,
      });
      this.loadData();
    } else {
      wx.showToast({
        title: '已全部加载',
        icon: 'success',
        duration: 1000
      });
    }
  },
  //加载数据
  loadData: function() {
    let memberCode = wx.getStorageSync('userInfo').memberCode
    http.request({
      url: '/memberAccount/findAccountRecordPage',
      data: {
        memberCode: memberCode, //商户编码
        operateType: this.data.current,
        pageSise: this.data.pageSize,
        pageNumber: this.data.pageNumber
      },
      success: (res) => {
        console.log(res)
        var data = this.data.list;
        if (res.totalElements > 0) {
          data = data.concat(res["content"]);
        }
        this.setData({
          list: data,
          allLoaded: res["totalPages"] <= this.data.pageNumber
        });
      }
    });
  },
  //完全重新加载数据
  reloadData: function() {
    this.setData({
      list: [],
      allLoaded: false,
      pageSize: 10,
      pageNumber: 1,
    });
    this.loadData();
  },
  //下拉刷新
  freshData: function() {
    console.log('下拉刷新')
    this.reloadData()

  },
  goAccountHis: function(e) {
    console.log(e);
    var comID = e.currentTarget.dataset.id;

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let memberCode = wx.getStorageSync('userInfo').memberCode;
    //获取屏幕高度用于触发上拉加载
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          winH: res.windowHeight
        });
      }
    });
    console.log(that.data.winH)

    console.log(memberCode)
    //初始化账户数据
    //获取账户金额
    http.request({
      url: '/member/findMemberAccount',
      data: {
        memberCode: memberCode
      },
      success: (result) => {
        that.setData({
          memberAccount: result

        })

      }
    });
    // console.log(that.data.memberAccount)
    this.loadData()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // 处理标签栏 滑动到顶部时  吸顶
  onShow: function() {
    let me = this;
    //1.获取tab的距离顶部高度
    const query = wx.createSelectorQuery();
    query.select('#tabs').boundingClientRect(function(res) {
      // console.log('res.top')

      me.data.tabTop = res.top
    }).exec();

  },
  // 2.监听页面滚动距离scrollTop
  onPageScroll: function(e) {
    // console.log('scrollTop')

    var that = this;

    // 3.当页面滚动距离scrollTop > tabTop菜单栏距离文档顶部的距离时，菜单栏固定定位
    if (e.scrollTop > that.data.tabTop) {
      // if (that.data.tabsFixed){
      //   return
      // }
      that.setData({
        tabsFixed: true,
        // showNavBar:true
      })
    } else {
      that.setData({
        tabsFixed: false,
        // showNavBar: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})